<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose title="排班" :width="896" :height="350" @ok="handleSubmit">
      <BasicForm @register="registerForm">
		   <template #deptIdSlot="{model,field}">
                <div style="width:150px">
                    <JSelectDept v-model:value="model[field]" @change="deptChange" :multiple="false" :checkable="false" :showButton="false" :defaultExpandLevel="1"></JSelectDept>
                </div>
           </template>
           <template #groupSlot="{ model, field }">
				<a-select
                    v-model:value="model[field]" @change="groupChange"
                    style="width: 180px" :fieldNames="{label:'name',value:'id' }"
                    :options="groupOptions"
                >
                </a-select>
			</template>
			 <template #personIdSlot="{ model, field }">
				<div style="width:150px">
					<JSelectPerson v-model:value="model[field]" 
					:showButtom="false" 
					:params="{groupId:groupId,status:1,delFlag:0}"
					>
					</JSelectPerson>
				</div>
			</template>
			
			<template #timePeriodSlot="{ model, field }">
                <a-select
                    v-model:value="model[field]"
                    style="width: 180px" :fieldNames="{label:'name',value:'id' }"
                    :options="timePeriodOptions"
                >
                </a-select>
			</template>
      </BasicForm>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {initFormSchema} from '../AttSchedule.data';
    import {attScheduleSubmit,personSchedulelist,getAllGroups,getTimePeriods} from '../AttSchedule.api';
    import JSelectPerson from '/@/components/Form/src/jeecg/components/JSelectPerson.vue';
    import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
	
	
    let groupOptions= ref([])
    let timePeriodOptions= ref([])
	
    // Emits声明
    const emit = defineEmits(['register','success']);
    
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate,getFieldsValue,validateFields}] = useForm({
        labelWidth: 150,
        schemas: initFormSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 12}
    });
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        setFieldsValue({
            deptId:data.deptId,
            sdate:data.sdate,
            edate:data.edate,
        });
        //getGroupOptions()
        
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const workDate = ref('')
    const groupId = ref('');
    
    const timePeriodId = ref('');
    let personOptions = ref([{
        personName: 'aaa',
        id:1
    }])
	let deptId = ref('')
    let targetId = ref('')
    
    function getPersonList(){
        let data = {
            sdate : workDate.value,
            edate : workDate.value,
        }
        personSchedulelist(data).then(res=>{
            personOptions.value = []
            res.records.forEach(item=>{

            })
            personOptions.value = res.records
            console.log(personOptions.value);
        })
    }
	function groupChange(e){
        groupId.value = e
        setFieldsValue({
               timePeriodId:'',
			   personIds:''
        })
		getTimePeriodOptions(e);
    }

    function deptChange(e){
        deptId.value = e? e.join(','): ''
		setFieldsValue({
               groupId:'',
               timePeriodId:'',
			   personIds:''
        })
        getGroupOptions();
    }
	
	function getGroupOptions(){
        getAllGroups({
            status:1,
            deptId:deptId.value,
            pageSize:1000000,
        }).then(res=>{
          groupOptions.value = res.records
        })
    }
	
	function getTimePeriodOptions(e){
      getTimePeriods({
        groupId:e? e : '',
      }).then(res=>{
        timePeriodOptions.value = res
      })
    }

    function originChange(e){
        console.log(e);
    }

    //表单提交事件
    async function handleSubmit(v) {
        try {
			await validateFields();
			let data = getFieldsValue();
			let params = Object.assign({}, {}, data);
			console.log('表单数据', params)
			await attScheduleSubmit(params, true)
            closeModal();
            //刷新列表
            emit('success');
        } finally {
            setModalProps({confirmLoading: false});
        }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
